<?php

/**
 * This is the model class for table "bbs_post".
 *
 * The followings are the available columns in table 'bbs_post':
 * @property string $id
 * @property string $topic_id
 * @property string $title
 * @property string $content
 * @property string $flag
 * @property string $member_id
 * @property string $author
 * @property string $created
 * @property string $updated
 */
class BbsPost extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BbsPost the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function beforeSave() {
		if ($this->isNewRecord){
			$this->created = new CDbExpression('NOW()');
		}
		$this->updated = new CDbExpression('NOW()');
		return parent::beforeSave();
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bbs_post';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('topic_id', 'required'),
			array('topic_id, member_id', 'length', 'max'=>10),
			array('title, author, updated', 'length', 'max'=>45),
			array('flag', 'length', 'max'=>8),
			array('content, created', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, topic_id, title, content, flag, member_id, author, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'topic_id' => 'Topic',
			'title' => 'Title',
			'content' => 'Content',
			'flag' => 'Flag',
			'member_id' => 'Member',
			'author' => 'Author',
			'created' => 'Created',
			'updated' => 'Updated',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('topic_id',$this->topic_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('flag',$this->flag,true);
		$criteria->compare('member_id',$this->member_id,true);
		$criteria->compare('author',$this->author,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}